from django.db.models.signals import post_save
from .models import WarehouseModel
from goods_info.models import GoodsInventoryModel,GoodsModel
from django.dispatch import receiver
import logging


@receiver(post_save,sender=WarehouseModel)
def create_goods_inventory(sender,instance,created,**kwargs):
    '''
  创建信号监控函数，创建新仓库之后，给所有的货品在当前的仓库添加新数据
    :param sender:
    :param instance:
    :param created:
    :param kwargs:
    :return:
    '''
    if created:
        if isinstance(instance,WarehouseModel):
            ids=GoodsModel.objects.values_list('id',flat=True).all()
            good_id_list=[]
            for i in ids:
                good_instance=GoodsInventoryModel(goods_id=i,warehouse_id=instance.id,warehouse_name=instance.name)
                good_id_list.append(good_instance)

            #批量新增函数 bulk_craete

            GoodsInventoryModel.objects.bulk_create(good_id_list)

        else:
            logging.info('不是WareHouseModel类型，不需要创建数据库数据')





